package com.beyondprototype.batch.config;

import java.util.List;

import org.springframework.batch.item.ItemProcessor;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.item.file.FlatFileItemReader;
import org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper;
import org.springframework.batch.item.file.mapping.DefaultLineMapper;
import org.springframework.batch.item.file.transform.DelimitedLineTokenizer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.FileSystemResource;

import com.beyondprototype.batch.model.CustomerMarker;
import com.beyondprototype.batch.service.CustomerMarkerService;

import lombok.extern.slf4j.Slf4j;

@Configuration
@Slf4j
public class GcduFlatDataBatchConfiguration {
    
    private String gcduDataFile = "/Users/xwp/Desktop/cngit/springbatch/usecase/gcdu-data.txt";
    
    
    @Autowired
    private CustomerMarkerService customerMarkerService;
    
    //load customer linkage records from a batch file
    @Bean
    @Qualifier("gcduFlatItemReader")
    public ItemReader<CustomerMarker> gcduFlatItemReader() {
    	
        FlatFileItemReader<CustomerMarker> reader = new FlatFileItemReader<CustomerMarker>();
        //reader.setResource(new ClassPathResource(gcduDataFile));
        reader.setResource(new FileSystemResource(gcduDataFile));
        reader.setLineMapper(new DefaultLineMapper<CustomerMarker>() {{
            setLineTokenizer(new DelimitedLineTokenizer() {{
                setNames(new String[] {"asOfDate","localIdentifer","market","globalIdentifer","markers"});
            }});

            setFieldSetMapper(new BeanWrapperFieldSetMapper<CustomerMarker>() {{
                setTargetType(CustomerMarker.class);
            }});
        }});
        
        return reader;
    }
    
    @Bean 
    @Qualifier("gcduFlatItemWriter")
    public ItemWriter<CustomerMarker> gcduFlatItemWriter() {
        return new ItemWriter<CustomerMarker>() {
            @Override
            public void write(List<? extends CustomerMarker> items) throws Exception {
                for (CustomerMarker item : items) {
                	log.info("Writing "+ item.toString());
                }
                
                customerMarkerService.create(items);
            }
        };
    }

    @Bean
    @Qualifier("gcduFlatItemProcessor")
    public ItemProcessor<CustomerMarker, CustomerMarker> gcduFlatItemProcessor() {
        return new ItemProcessor<CustomerMarker, CustomerMarker>() {
            @Override
            public CustomerMarker process(CustomerMarker item) throws Exception {
            	
            	log.info("Processing "+item.toString());
            	
            	item.setStatus("Processing");
            	
                return item;
            }
        };
    }
}
